﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Security.Cryptography;
using System.Text;

namespace wuliuback
{
    public partial class QueryFavorite : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = Request["type"]; //货源1，车源2，用户3，货源和车源4
            string user_id = Request["user_id"];
            string pageSize = Request["page_size"];
            string minId = Request["min_id"];

            string code = "";
            string msg = "";
            string favorite = "";

            string curMinId = "";

            String sql = "";
            DbHelper dbHelper = new DbHelper();
            
            if (type=="2")
            {
                sql = "select top (" + pageSize + ") t.*,2 as type,t.truck_id as id,f.type as favorite_type,f.id as f_id from t_truck t,t_favorite f where f.status>-1 and t.user_id=f.user_id ";
                if (type != null && type.Length > 0)
                {
                    sql = sql + "and f.type='" + type + "'";
                }
                if (user_id != null && user_id.Length > 0)
                {
                    sql = sql + " and f.user_id = '" + user_id + "' ";
                }
                if (minId != null && minId.Length > 0)
                {
                    sql = sql + " and f.id < " + minId;
                }
                sql = sql + " order by f.time desc";
                string sql1 = "select min(f_id) as min_id from (" + sql + ") t";


                DataSet dataSet = dbHelper.Select(sql1);
                DataTable table = dataSet.Tables[0];
                if (table.Rows.Count > 0)
                {
                    curMinId = table.Rows[0][0].ToString();
                }
            }
            else if (type == "1")
            {
                sql = "select top (" + pageSize + ") t.*,1 as type,t.goods_id as id,f.type as favorite_type,f.id as f_id from t_goods t,t_favorite f where f.status>-1 and t.status>-1 and t.user_id=f.user_id ";
                if (type != null && type.Length > 0)
                {
                    sql = sql + "and f.type='" + type + "'";
                }
                if (user_id != null && user_id.Length > 0)
                {
                    sql = sql + " and f.user_id = '" + user_id + "' ";
                }
                if (minId != null && minId.Length > 0)
                {
                    sql = sql + " and f.id < " + minId;
                }
                sql = sql + " order by f.time desc";
                string sql1 = "select min(f_id) as min_id from (" + sql + ") t";


                DataSet dataSet = dbHelper.Select(sql1);
                DataTable table = dataSet.Tables[0];
                if (table.Rows.Count > 0)
                {
                    curMinId = table.Rows[0][0].ToString();
                }
            }
            else if (type == "3")
            {
                sql = "select top (" + pageSize + ") t.*,f.type as favorite_type,f.id as f_id from t_user t,t_favorite f where f.status>-1 and t.user_id=f.user_id ";
                if (type != null && type.Length > 0)
                {
                    sql = sql + "and f.type='" + type + "'";
                }
                if (user_id != null && user_id.Length > 0)
                {
                    sql = sql + " and f.user_id = '" + user_id + "' ";
                }
                if (minId != null && minId.Length > 0)
                {
                    sql = sql + " and t.id < " + minId;
                }
                sql = sql + " order by t.register_date desc";
                string sql1 = "select min(f_id) as min_id from (" + sql + ") t";


                DataSet dataSet = dbHelper.Select(sql1);
                DataTable table = dataSet.Tables[0];
                if (table.Rows.Count > 0)
                {
                    curMinId = table.Rows[0][0].ToString();
                }
            }
            else
            {
                sql = "select top (" + pageSize + ") t.*,f.type as favorite_type,f.id as f_id from v_goods_truck t,t_favorite f where f.status>-1 and t.status>-1 and t.user_id=f.user_id ";
                
                sql = sql + "and (f.type='1' or f.type='2') ";
                
                if (user_id != null && user_id.Length > 0)
                {
                    sql = sql + " and f.user_id = '" + user_id + "' ";
                }
                if (minId != null && minId.Length > 0)
                {
                    sql = sql + " and f.id < " + minId;
                }
                sql = sql + " order by f.time desc";
                string sql1 = "select min(f_id) as min_id from (" + sql + ") t";


                DataSet dataSet = dbHelper.Select(sql1);
                DataTable table = dataSet.Tables[0];
                if (table.Rows.Count > 0)
                {
                    curMinId = table.Rows[0][0].ToString();
                }
            }

            DataSet dataSet1 = dbHelper.Select(sql);
            DataTable table1 = dataSet1.Tables[0];
            
            code = "0";
            favorite = JsonUtil.DataTableToJson(table1);

            Response.Output.WriteLine("{\"code\":\"" + code + "\",\"msg\":\"" + msg
                                    + "\",\"min_id\":\"" + curMinId + "\",\"page_size\":\"" + pageSize + "\",\"type\":\"" + type
                                    + "\",\"user_id\":\"" + user_id + "\",\"info\":" + favorite + "}");
        }
    }
}
